from app.infrastructure import db_pool
from app.config.api_config import DB_QUERY_CONFIG
from sqlalchemy import text
import json

def main():
    print("DB连接配置:")
    print(json.dumps(DB_QUERY_CONFIG, indent=2))
    
    print("\n正在连接数据库...")
    try:
        # 使用SQLAlchemy执行查询
        with db_pool.get_connection() as conn:
            print("数据库连接成功!")
            
            print("\n获取所有表:")
            result = conn.execute(text("SHOW TABLES"))
            tables = result.fetchall()
            for table in tables:
                print(f"- {table[0]}")
            
            print("\n检查sys_org表:")
            result = conn.execute(text("SHOW TABLES LIKE 'sys_org'"))
            if result.fetchone():
                print("sys_org表存在")
                
                # 检查表结构
                result = conn.execute(text("DESCRIBE sys_org"))
                columns = result.fetchall()
                print("\n表结构:")
                for col in columns:
                    print(f"- {col[0]} ({col[1]})")
                    
                # 检查数据
                result = conn.execute(text("SELECT COUNT(*) FROM sys_org"))
                count = result.scalar()
                print(f"\n表中有 {count} 条记录")
                
                if count > 0:
                    result = conn.execute(text("SELECT id, name FROM sys_org LIMIT 5"))
                    rows = result.fetchall()
                    print("\n示例数据:")
                    for row in rows:
                        print(f"ID: {row[0]}, 名称: {row[1]}")
            else:
                print("sys_org表不存在!")
                
                # 检查是否有类似的表
                result = conn.execute(text("SHOW TABLES LIKE '%org%'"))
                org_tables = result.fetchall()
                if org_tables:
                    print("\n找到类似的表:")
                    for table in org_tables:
                        print(f"- {table[0]}")
                        
                        # 检查第一个类似表的结构
                        result = conn.execute(text(f"DESCRIBE {table[0]}"))
                        columns = result.fetchall()
                        print(f"\n{table[0]}表结构:")
                        for col in columns:
                            print(f"- {col[0]} ({col[1]})")
                        break
                else:
                    print("未找到任何包含'org'的表")
    except Exception as e:
        print(f"错误: {str(e)}")
        
if __name__ == "__main__":
    main() 